package com.ld.hpfs.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.hpfs.dao.UploadMapper;
import com.ld.hpfs.service.UploadService;

@Service
public class UploadServiceImpl implements UploadService{
	private static final Logger logger = LoggerFactory.getLogger(UploadServiceImpl.class);
	@Autowired
	private UploadMapper uploadMapper;

	// 文件所在的层数  
    private int fileLevel;  
      
	
	@Override
	public ArrayList<HashMap<String,Object>> getImportLog1(HashMap<String,Object> param)
	{
		int count = uploadMapper.getImportLogCount1(param);
		if(count == 0){
			return null;
		}else{
			param.put("pageCount", count);
			return uploadMapper.getImportLog1(param);
		}
	}	
	
	@Override
	public ArrayList<HashMap<String,Object>> getImportLog2(HashMap<String,Object> param)
	{
		int count = uploadMapper.getImportLogCount2(param);
		if(count == 0){
			return null;
		}else{
			param.put("pageCount", count);
			return uploadMapper.getImportLog2(param);
		}
	}	
	
	@Override
	public ArrayList<HashMap<String,Object>> getImportLog3(HashMap<String,Object> param)
	{
		int count = uploadMapper.getImportLogCount3(param);
		if(count == 0){
			return null;
		}else{
			param.put("pageCount", count);
			return uploadMapper.getImportLog3(param);
		}
	}	
	
	@Override
	public ArrayList<HashMap<String,Object>> getUploadFileList(HashMap<String, Object> param){
		return uploadMapper.getUploadFileList(param);
	}
	
	@Override
	public int insertImportLog(HashMap<String, Object> param)
	{
		try{
			uploadMapper.insertImportLog(param);
			return 0;
		}catch(Exception ex){
			// logger.error(ex.getMessage());
			return -1;
		}
	}
	
	@Override
	public int insertImportLogTAX(HashMap<String, Object> param)
	{
		try{
			uploadMapper.insertImportLogTAX(param);
			return 0;
		}catch(Exception ex){
			// logger.error(ex.getMessage());
			return -1;
		}
	}
	
	public void ReLoadHardDiskFiles(String path,String code, int user_id){
		uploadMapper.delFileDir(user_id);
		path = path + code;
		readFile(path,user_id);
	}
	
	@Override
	public ArrayList<HashMap<String,Object>> getFileDirInfo(String path, String code, int user_id)
	{
		return uploadMapper.getDirInfo(user_id);
	}
	
	@Override
	public String getCompanyCode(int id){
		return uploadMapper.getCompanyCode(id);
	}
	
	/** 
     * 输出给定目录下的文件，包括子目录中的文件 
     * @param dirPath 给定的目录 
     */  
    private void readFile(String dirPath,int user_id) {  
            // 建立当前目录中文件的File对象  
            File file = new File(dirPath);  
            // 取得代表目录中所有文件的File对象数组  
            File[] list = file.listFiles();  //获取实际目录文件
            // 遍历file数组  
            for (int i = 0;list != null &&  i < list.length; i++) {  
                    if (list[i].isDirectory()) {  
                    	insertFileDir(list[i].getName(), fileLevel,list[i].getParentFile().getAbsolutePath(),list[i].getAbsolutePath(),1,user_id);  
                            fileLevel ++;  
                            // 递归子目录  
                            readFile(list[i].getPath(),user_id);  
                            fileLevel --;  
                    } else {  
                    	insertFileDir(list[i].getName(), fileLevel,list[i].getParentFile().getAbsolutePath(),list[i].getAbsolutePath(),0,user_id); 
                    }  
            }  
    }  
    
    /** 
     * 生成输出格式 
     * @param name 输出的文件名或目录名 
     * @param level 输出的文件名或者目录名所在的层次 
     * @return 输出的字符串 
     */  
    private void insertFileDir(String name, int level,String parent_name,String file_dir,int is_dir,int user_id) {  
    	HashMap<String,Object> param = new HashMap<String,Object>();
    	param.put("file_dir", file_dir);
    	param.put("file_name", name);
    	param.put("parent_dir", parent_name);
    	param.put("level", level);
    	param.put("is_dir", is_dir);
    	param.put("user_id", user_id);
    	uploadMapper.insertFileDir(param);
    } 
       
    /*
     * 删除批次
     */
    @Override
    public int deletePlanCode(HashMap<String, Object> param)
    {
    	try{
    		uploadMapper.deletePlanCode(param);
			return 0;
		}catch(Exception ex){
			logger.error(ex.getMessage());
			return -1;
		}
    }
    
    /**
     * 获取本地导入文件标题列表
     */
    @Override
    public ArrayList<HashMap<String,Object>> getLocalFileTitle(HashMap<String, Object> param){
    	return uploadMapper.getLocalFileTitle(param);
    }
    
    /**
     * 设置本地导入文件标题列表
     */
    @Override
    public int setLabelName(HashMap<String, Object> param){
    	try{
    		uploadMapper.insertLabelName(param);
    	}catch(Exception ex){
    		try{
    			uploadMapper.updateLabelName(param);
    		}catch(Exception ex2){
    			logger.error(ex2.getMessage());
    			return -1;
    		}
    	}
    	return 0;
    }
}
